﻿/// <reference path="../Framework/BrowserApi.js" />
/// <reference path="../Framework/Toolbar/Toolbar.css" />
/// <reference path="Content.js" />
/// <reference path="Global.js" />
/// <reference path="MouseAndKeyboardEvents.js" />
/// <reference path="SelectionProcessing.js" />
/// <reference path="VirtualScrolling.js" />
/// <reference path="ToolbarItems.js" />

var Spreadsheet = new function ()
{
    // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *    

    // Create the spreadsheet in the browser, and load the default content file.
    this.Initialize = function ()
    {
        window.removeEventListener('load', Spreadsheet.Initialize, false);

        ScrollPanel = document.getElementById("ScrollPanel");
        SpreadsheetTable = document.getElementById("SpreadsheetTable");
        SpreadsheetBody = document.getElementById("SpreadsheetBody");
        SpreadsheetPanel = document.getElementById("SpreadsheetPanel");
        ScrollPanelContent = document.getElementById("ScrollPanelContent");
        CellHighlight = document.getElementById("CellHighlight");
        InstructionsPanel = document.getElementById("InstructionsPanel");
        SpreadsheetPlaceholder = document.getElementById("SpreadsheetPlaceholder");

        // Detecting html 5 support
        if (BrowserApi.Html5Support == false)
        {
            alert('This browser does not support HTML5, please try the latest release from Internet Explorer, Google Chrome, Firefox, Safari, or Opera')
        //    return;
        }

        Spreadsheet.CreateSpreadsheet()

        MouseAndKeyboardEvents.Initialize();
        VirtualScrolling.Initialize();
        window.onresize = Spreadsheet.OnResizeEventHandler

        BrowserApi.DisableSelectAndDrag(SpreadsheetPlaceholder)
        BrowserApi.DisableSelectAndDrag(SpreadsheetBody)
        BrowserApi.DisableSelectAndDrag(CellHighlight)
    }

    // Create the spreadsheet to cover the maximum visible area that the browser can be maximized too.
    this.CreateSpreadsheet = function ()
    {
        this.CreatingSpreadsheet = true;

        this.CurrentRowsHeight = 0;
        this.CurrentColumnWidth = 0;

        this.ScrollbarSize = BrowserApi.FindScrollbarSize();
        this.OnResizeEventHandler();
        this.CreateSpreadsheetRows();

        this.CreatingSpreadsheet = false;
        this.InitializeAndLoadCompleted();
    }

    // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *    

    this.CurrentRowsHeight = 0
    this.CurrentColumnWidth = 0
    this.StandardRowHeight = 23         // 22 + 1 for the border.
    this.StandardRowNumberWidth = 56    // 55 + 1 for the border.
    this.StandardColumnWidth = 26       // 25 + 1 for the border.

    // Create the spreadsheet rows and set the table heights to the total number of spreadsheet rows.
    this.CreateSpreadsheetRows = function ()
    {
        this.CurrentRowsHeight = 0
        var rowId = 0
        while (this.CurrentRowsHeight < screen.height)
        {
            var tableRow = document.createElement("tr")
            tableRow.style.height = this.StandardRowHeight + "px"

            this.CreateSpreadsheetCells(tableRow, rowId);
            this.CurrentRowsHeight += this.StandardRowHeight;
            SpreadsheetBody.appendChild(tableRow);
            rowId++;
        }
        SpreadsheetTable.style.height = this.CurrentRowsHeight + "px";
    }

    // Create the spreadsheet cells and set the table width to the total number of spreadsheet columns.
    // Create another layer of span elements over the spreadsheet cells to display the data.
    this.CreateSpreadsheetCells = function (tableRow, rowId)
    {
        this.CurrentColumnWidth = 0;
        var columnId = 0
        while (this.CurrentColumnWidth < screen.width)
        {
            var cellText = document.createElement("span");
            cellText.style.position = "absolute"
            cellText.style.top = this.CurrentRowsHeight + 5 + "px"
            cellText.style.left = this.CurrentColumnWidth + 3 + "px"
            cellText.style.zIndex = 3
            cellText.style.overflow = 'hidden'
            cellText.className = 'SpreadSheetText'
            BrowserApi.DisableSelectAndDrag(cellText)
            cellText.id = "Text_" + rowId + "_" + columnId
            InstructionsPanel.appendChild(cellText);

            var tableCell = document.createElement("td");

            if (rowId == 0)
            {
                if (this.CurrentColumnWidth == 0)
                {
                    tableCell.className = "RowColumnSplitCellStyle";
                    this.CurrentColumnWidth += this.StandardRowNumberWidth;
                }
                else
                {
                    tableCell.className = "ColumnHeaderCell";
                    this.CurrentColumnWidth += this.StandardColumnWidth;
                    cellText.className = 'ColumnHeaderCellText'
                }
            }
            else
            {
                if (this.CurrentColumnWidth == 0)
                {
                    tableCell.className = "RowNumberCellStyle";
                    this.CurrentColumnWidth += this.StandardRowNumberWidth;
                    cellText.className = 'RowNumberCellText'
                }
                else
                {
                    tableCell.className = "NormalCellStyle";
                    this.CurrentColumnWidth += this.StandardColumnWidth;
                }
            }

            tableRow.appendChild(tableCell);

            columnId++;
        }

        SpreadsheetTable.style.width = this.CurrentColumnWidth + "px";
    }

    // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

    // Once the default content file is loaded and the table is ready on the screen, complete the initialization.
    this.InitializeAndLoadCompleted = function ()
    {
        if (this.CreatingSpreadsheet != false)
        {
            return;
        }

        ScrollPanelContent.style.height = (SpreadsheetContent.TotalRows * this.StandardRowHeight) + "px";
        ScrollPanelContent.style.width = (SpreadsheetContent.MaximumColumns * this.StandardColumnWidth) + "px";

        SelectionProcessing.SelectedArea = new Object()
        SelectionProcessing.SelectedArea.SelectedX = this.StandardRowNumberWidth + 1
        SelectionProcessing.SelectedArea.SelectedY = this.StandardRowHeight + 1
        SelectionProcessing.SelectedArea.CurrentX = this.StandardRowNumberWidth + 1
        SelectionProcessing.SelectedArea.CurrentY = this.StandardRowHeight + 1
        CellHighlight.style.visibility = 'visible'
        SelectionProcessing.SelectArea()

        VirtualScrolling.OnScrollEventHandler();
    }

    // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

    // Reposition some of the html elements on resize.
    this.OnResizeEventHandler = function ()
    {
        SelectionProcessing.SelectingCells = false
        BrowserApi.ScheduleFunction('Spreadsheet.OnResizeEventHandler_Completed', '', 50)
    }

    this.OnResizeEventHandler_Completed = function ()
    {
        SpreadsheetPanel.style.bottom = this.ScrollbarSize + "px";
        SpreadsheetPanel.style.right = this.ScrollbarSize + "px";
    }

    // Create a new file and save the old one if the user chooses too.
    this.NewEventHandler = function ()
    {
        // var yesNo = confirm('Do you want to save the current changes?');
      //  var test = chrome.windows.create('../Framework/FileDialog.htm');
        window.showModalDialog('../Framework/FileDialog.htm', 'Task Remarks', 'dialogWidth: 500px; dialogHeight: 500px; scrolling: no; status: no; resizable: no; help: no;')
    }

    // Open a new file and save the old one if the user chooses too.
    this.OpenEventHandler = function ()
    {
        alert('Open');
    }

    this.SaveEventHandler = function ()
    {
        alert('save');
    }

    this.SaveAsEventHandler = function ()
    {
        alert('save as');
    }

    this.CutEventHandler = function ()
    {
        alert('cut');
    }

    this.CopyEventHandler = function ()
    {
        alert('copy');
    }
}

// Wait until the entire page is loaded with all external scripts and files, then initialize the page.
window.addEventListener('load', Spreadsheet.Initialize, false);